package indi.kider.server.woniubus.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.constant.StatusCons;
import indi.kider.common.model.apabus.Bus;
import indi.kider.common.model.apabus.CompleteBean;
import indi.kider.common.model.apabus.Dispatch;
import indi.kider.common.model.apabus.ServiceDelivery;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.Tenant;
import indi.kider.server.woniubus.service.BusService;
import indi.kider.server.woniubus.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pers.vankid.common.model.base.CommonResponse;
import pers.vankid.common.utils.page.PageRequest;
import pers.vankid.common.utils.page.PageResponse;

import java.util.List;

/**
 * 服务控制器
 */
@RestController
@RequestMapping(value = "bus")
public class BusController {

    @Autowired
    private BusService busService;
    @Autowired
    private AuthcService authcService;
    /**
     * 自动查询修改warningState状态
     **/
    @PostMapping(value = "updateBusWarningState")
    public CommonResponse updateBusWarningState() throws Exception {
        return busService.updateBusWarningState();
    }

    /**
     * @api {GET} /{houseId}
     * @apiGroup bus
     * @apiDescription 根据houseId获取租客信息
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/bus/1
     * @apiSuccessExample {json} 返回样例:
     * {"status":"200","msg":"查询成功！","data":{"id":1,"name":"shixj","cardId":12212,"phone":"1221","nationality":"32","professional":"23","wxopenId":"121"}}
     */
    @GetMapping("{houseId}")
    public CommonResponse getTenantListByHouseId(@PathVariable Long houseId) {
        try {
            return CommonResponse.buildGetSuccessResponse(busService.getTenantListByHouseId(houseId));
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
    /**
     * @api {POST} /serviceDelivery
     * @apiGroup bus
     * @apiDescription 服务提供
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/bus/serviceDelivery
     * {"busType":1,"busdeis":"电灯坏了","houseId":1,"waiterId":1,tenantId:1}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": ""
     * }
     */
    @PostMapping("/serviceDelivery")
    public CommonResponse serviceDelivery(@RequestBody ServiceDelivery serviceDelivery) {
        try {
            Employee employee = authcService.getCurrentUser().getData();
            return busService.serviceDelivery(serviceDelivery,employee);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }

    /**
     * @api {POST} /getPageServiceList
     * @apiGroup wnbus
     * @apiDescription 根据字段查询获取服务列表（分页）
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/getPageServiceList
     */
    @PostMapping("/getPageServiceList")
    public CommonResponse<PageResponse<List<Bus>>> getPageServiceList(@RequestBody PageRequest<Bus> request) {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee!=null&&!employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            request.getQuery().setEnterpriseId(employee.getEnterpriseId());
        }
        request.getQuery().setDelState(StatusCons.VALID);
        return CommonResponse.buildGetSuccessResponse(busService.queryBusList(request));
    }

    /**
     * @api {PUT} /dispatch
     * @apiGroup wnbus
     * @apiDescription 服务派单
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/dispatch
     * {"busId":null,"waiterId":null,des:"问题描述"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/dispatch")
    public CommonResponse dispatch(@RequestBody Dispatch dispatch) {
        try {
            return busService.dispatch(dispatch);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }


    /**
     * @api {PUT} /complete
     * @apiGroup wnbus
     * @apiDescription 处理服务完成
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/wnbus/complete
     * {"busId":null,"resultTime":"处理完成时间","result":"处理结果"}
     * @apiSuccessExample {json} 返回样例:
     * {
     * "status": "201",
     * "msg": "保存成功！",
     * "data": 1
     * }
     */
    @PutMapping("/complete")
    public CommonResponse complete(@RequestBody CompleteBean completeBean) {
        try {
            return busService.complete(completeBean);
        } catch (Exception e) {
            return CommonResponse.buildExceptionResponse(e);
        }
    }
}
